package qf.sail.controller;



import qf.sail.contents.CodeMsg;
import qf.sail.entity.Account;
import qf.sail.service.AccountService;
import qf.sail.service.impl.AccountServiceImpl;
import qf.sail.vo.ResultVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 用户注册
 */
@WebServlet("/register")
public class RegisterServlet extends BaseServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String phone = req.getParameter("phone");
        String password = req.getParameter("password");
        AccountService accountService = new AccountServiceImpl();

        if(phone == null || phone.trim().length() == 0){
            repError(resp, CodeMsg.PHONE_NULL);
            return;
        }
        if(password == null || password.trim().length() == 0){
            repError(resp,CodeMsg.PASSWORD_NULL);
            return;
        }
        if(accountService.findAccountByPhone(phone) != null){
            repError(resp,CodeMsg.HAVE_ACCOUNT);
            return;
        }
        Account account = new Account();
        account.setPhone(phone);
        account.setPassword(password);

        int result = accountService.addAccount(account);

        if(result != 1){
            repError(resp,CodeMsg.REG_FAIL);
            return;
        }

        ResultVO resultVO = new ResultVO();
        resultVO.addData("account",account);
        repSuccess(resp,resultVO);

    }
}
